# 引入django框架中http模块中HttpResponse函数
import math

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect


from mydjango.models import Users, Goods, GoodsCars


def hw(requset):
    return HttpResponse()


def index(requset):
    # name = requset.GET.get('name')
    if requset.session.get('uid', None):
        name = Users.objects.filter(id=requset.session['uid'])[0].username
    else:
        name = None

    search = requset.GET.get('search')
    if search:
        goods = Goods.objects.filter(goodsname__contains=search)
    else:
        goods = Goods.objects.all()
    # 接受前端传递的页码参数
    pagelist = range(1, math.ceil(len(goods)/5)+1)
    page = requset.GET.get('page')
    if page:
        # 有值 字符串转化为 整数
        page = int(page)
    else:
        # 无值 默认展示第一页
        page = 1
    # 截取数据查询的结果，前5个列表切片
    goods = goods[5*(page-1):5*page]
    return render(requset, "index.html", {'name': name, 'goods': goods, 'pagelist': pagelist})


def login(requset):
    if requset.POST:
        username = requset.POST.get('username')
        password = requset.POST.get('password')
        result = Users.objects.filter(username=username)
        if result:
            if result[0].userpwd == password:
                # 设置session
                requset.session['uid'] = result[0].id
                return redirect('/index/?name='+result[0].username)
            else:
                return render(requset, "login.html")
        else:
            return render(requset, "register.html")
            # return render(requset, "login.html")
    return  render(requset, "login.html")


def register(requset):
    if requset.POST:
        username = requset.POST.get('username')
        userpwd = requset.POST.get('userpwd')
        # 插入数据库
        # 将用户类实例化
        # 1.
        # user = Users(username=username, userpwd=userpwd)
        # user.save()
        # 2.
        Users.objects.create(id=None, username=username, userpwd=userpwd)
        return render(requset, 'login.html')
    return render(requset, 'register.html')


def nav(requset):
    one = [
        {'院系设置': [
            {'理工农学院': ['软件工程', '计算机科学与技术', '数字媒体']},
            {'文艺法学院': ['园林艺术', '土木工程', '舞蹈专业']},
            {'经管学院': ['会计', '金融', '电子商务']},
            {'医学院': ['护理', '临床']},
        ]},
        {'师资团队': [
            {'师资团队1': ['教师1', '教授1', '研究生1']},
            {'师资团队2': ['教师2', '教授2', '研究生2']},
            {'师资团队3': ['教师3', '教授3', '研究生3']}
        ]},
        {'科学研究': [
            {'科学研究1': ['研究1', '实验室1']},
            {'科学研究2': ['研究2', '实验室2']}
        ]},
        {'招生就业': [
            {'招生就业': ['企业1']}
        ]}

    ]

    return render(requset, "nav.html", {'one': one})

def detail(requset):
    goodsid = requset.GET.get('goodsid')

    if requset.POST:
        goodsid = requset.POST.get('goodsid')
        uid = requset.session['uid']
        car = GoodsCars(goodsid=goodsid, buy_count=1, uid=uid)
        car.save()
    goods = Goods.objects.filter(goodsid=goodsid)
    # car_gid = requset.GET.get('car_gid')
    # if car_gid:
    #   GoodsCars.objects.create(cid=None, goodsid=car_gid, buy_count=1)
    #    goods = Goods.objects.filter(goodsid=car_gid)
    return  render(requset, "detail.html", {'goods': goods})

def shoppingcar(requset):
    # 先判断 判断是否登录 用requset.session对象中是否存在键uid 的值
    if requset.session.get('uid', None): # 有值 Ture 说明登录过
        # 获取 session中的uid
        uid = requset.session['uid']
        # 数据库操作 查询操作 购物车表
        cars = GoodsCars.objects.filter(uid=uid)
        # 数据库查询结果 cars[{cid,gid,buy_count}]
        # 设定 响应前端页面的所有购物车商品数据的列表
        goods = []
        for item in cars:
            good = Goods.objects.filter(goodsid=item.goodsid)[0]
            good.buy_count = item.buy_count
            good.allprice = good.goodsprice * item.buy_count
            goods.append(good)
        return render(requset, "shoppingcar.html", {'goods': goods})
    else:# 无值 False 说明没登录
        return redirect('/login/')

def loginout(requset):
    # 业务处理-->数据处理
    # 删除session对象中uid键值对 实质对数据库中的session表修改session——data字段值
    # requset.session.pop('uid')
    # del requset.session['uid']
    # requset.session.clear()

    # 删除数据库中session表对应的记录/数据库
    requset.session.delete(requset.session.session_key)
    # requset.session.flush()
    return redirect('/index')